import time

class ControlPanel:
    def __init__(self, password="1234"):
        self.password = password  # 设定正确密码，默认为 1234
        self.input_password = ""  # 用户输入的密码
        self.failed_attempts = 0  # 错误尝试次数
        self.locked_until = 0     # 锁定到的时间点
        self.state = "Reading Password"  # 初始状态是读取密码

    def reset(self):
        """重置控制面板，清除用户输入、错误尝试次数"""
        self.input_password = ""
        self.failed_attempts = 0
        self.state = "Reading Password"

    def enter_password(self, char):
        """模拟用户输入密码字符"""
        if self.is_locked():
            print("Control panel is locked. Please wait.")
            return
        
        # 如果用户输入字符，更新密码并检查密码长度
        self.input_password += char
        print(f"Entered password: {self.input_password}")
        
        # 检查密码长度是否达到 4 位
        if len(self.input_password) == 4:
            self.validate_password()
        
    def validate_password(self):
        """验证用户输入的密码"""
        if self.input_password == self.password:
            print("Password is correct.")
            self.state = "System Function Selection"  # 进入系统功能选择状态
            self.failed_attempts = 0  # 重置错误次数
            self.input_password = ""
        else:
            print("Incorrect password.")
            self.failed_attempts += 1
            if self.failed_attempts >= 3:
                self.lock_panel()  # 如果失败次数达到 3 次，进入锁定状态
            else:
                self.state = "Incorrect Password"  # 密码错误但未达到 3 次，继续输入
    
    def lock_panel(self):
        """进入锁定状态"""
        print("Control panel is locked due to 3 incorrect attempts.")
        self.state = "Locked"
        self.locked_until = time.time() + 120  # 锁定 120 秒
        self.input_password = ""  # 清空输入的密码
    
    def is_locked(self):
        """检查控制面板是否被锁定"""
        if self.state == "Locked":
            if time.time() >= self.locked_until:
                print("Lock time is over. You can now input password again.")
                self.state = "Reading Password"
                return False
            return True
        return False
    
    def show_state(self):
        """显示当前控制面板的状态"""
        print(f"Current state: {self.state}")

# 模拟 SafeHome 控制面板行为
def simulate_control_panel():
    control_panel = ControlPanel()  # 创建一个控制面板实例
    control_panel.show_state()  # 显示初始状态

    # 模拟用户输入密码
    control_panel.enter_password('1')
    control_panel.enter_password('2')
    control_panel.enter_password('3')
    control_panel.enter_password('4')
    control_panel.show_state()

    # 模拟密码输入错误的情况
    control_panel.enter_password('1')
    control_panel.enter_password('2')
    control_panel.enter_password('5')  # 错误密码
    control_panel.enter_password('6')  # 错误密码
    control_panel.enter_password('7')  # 错误密码
    control_panel.show_state()

    # 等待锁定解除
    time.sleep(121)  # 等待超过锁定时间
    control_panel.show_state()
    control_panel.enter_password('1')
    control_panel.enter_password('2')
    control_panel.enter_password('3')
    control_panel.enter_password('4')
    control_panel.show_state()

# 运行模拟
simulate_control_panel()
